package com.lz.sports.vm.config

import android.text.Editable
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lz.comm.SP
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.sports.core.*

class ScoreRuleViewModel : BaseViewModel<BaseModel>() {


    val projectName = ProjectManager.examProjectName()



    //男生考试分
    val examCountB = MutableLiveData<String>()
    //女生考试分
    val examCountG = MutableLiveData<String>()
    //考试时间
    val examTime = MutableLiveData<String>()

    //分数线输入
    private var bLevelEnabled = true
    private var gLevelEnabled = true
    private var timeEnabled = true
    //保存按钮
    val saveEnabled = MutableLiveData(true)

    override fun onCreate(owner: LifecycleOwner) {
        examCountB.value = "${SP.getInt(K_RULE_SCORE_PASS_B, v_ruleScorePass_b)}"
        examCountG.value = "${SP.getInt(K_RULE_SCORE_PASS_G, v_ruleScorePass_g)}"
        examTime.value = "${SP.getInt(K_RULE_SCORE_TIME, v_ruleScoreTime)}"
    }

    fun afterBLevelTextChanged(editable: Editable) {
        val level = editable.toString().trim().toIntOrNull()
        bLevelEnabled = level != null && level > 0
        saveEnabled.value = bLevelEnabled && gLevelEnabled && timeEnabled
    }

    fun afterGLevelTextChanged(editable: Editable) {
        val level = editable.toString().trim().toIntOrNull()
        gLevelEnabled = level != null && level > 0
        saveEnabled.value = bLevelEnabled && gLevelEnabled && timeEnabled
    }

    fun afterTimeTextChanged(editable: Editable) {
        val time = editable.toString().trim().toIntOrNull()
        timeEnabled = time != null && time > 0
        saveEnabled.value = bLevelEnabled && gLevelEnabled && timeEnabled
    }

    fun save() {
        SP.setInt(K_RULE_SCORE_PASS_B, examCountB.value!!.trim().toInt())
        SP.setInt(K_RULE_SCORE_PASS_G, examCountG.value!!.trim().toInt())
        SP.setInt(K_RULE_SCORE_TIME, examTime.value!!.trim().toInt())
    }
}